package demo;

import static demo.Const.GRID_SIZE;
import static demo.Const.TEAM_BLUE;
import static jtech.Constantes.KEY_ESC;
import static jtech.Constantes.MOUSE_BUTTON_LEFT;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import jtech.Game;
import jtech.GameAction;
import demo.miner.ClassEntity;
import demo.miner.Game;
import demo.miner.Team;

public class TilesTest extends Game {

    public static void main(String[] args) {
        System.setProperty("sun.awt.enableExtraMouseButtons", "true");
        new TilesTest().startgame();
    }
    
    private GameAction quit = new GameAction("Sair");
    
    private Game world = new Game();
    
    static {
    	ClassEntity.define(
				"className=Peon",
				"spriteGraphicpeon.png",
				"baseHitPoints = 40",
				"baseArmor = 0",
				"baseDamage = 4",
				"attackRange = 4",
				"baseReloadTime = 3",
				"baseWalkSpeed = 4",
				"baseRadius = 16",
				"foodUsed = 1",
				"goldCost = 200");
    	
    	ClassEntity.define(
    			"className = Footman",
            	"spriteGraphic = footman.png",
            	"baseHitPoints = 120",
            	"baseArmor = 2",
            	"baseDamage = 16",
            	"attackRange = 6",
            	"baseReloadTime = 1",
            	"baseWalkSpeed = 5",
            	"baseRadius = 16",
            	"foodUsed = 2",
            	"goldCost = 400");
    	
    	ClassEntity.define(
    			"className = Archer",
            	"spriteGraphic = archer.png",
            	"baseHitPoints = 100",
            	"baseArmor = 0",
            	"baseDamage = 20",
            	"attackRange = 64",
            	"baseReloadTime = 2",
            	"baseWalkSpeed = 5",
            	"baseRadius = 16",
            	"foodUsed = 2",
            	"goldCost = 600");
    	
    	Team.define(
    			"id=0",
    			"name=Neutral",
    			"color= 172,192,218");

    	Team.define(
    			"id=1",
    			"name=Blue",
    			"color=51,76,204");
    	
    	Team.define(
    			"id=2",
    			"name=Red",
    			"color=255,0,0");
    	
    	Team.define(
    			"id=3",
    			"name=White",
    			"color=255,255,255");
    	
    	Team.define(
    			"id=4",
    			"name=Yellow",
    			"color=255,255,0");
    	
    }

    private int[][] currentMapTerrainGrid = {
    		{0, 0, 0, 8, 1, 2, 0, 0, 0, 0},
    		{0, 0, 0, 7, 9, 3, 0, 0, 0, 0},
    		{0, 0, 0, 6, 5, 4, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 10, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 14, 0, 0, 0, 0},
    		{0, 0, 0, 13, 15, 16, 15, 11, 0, 0},
    		{0, 0, 0, 0, 0, 14, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 12, 0, 0, 0, 0},
    };
    
    Color[] greens = {
		new Color(2, 150, 35, 255),
		new Color(2, 148, 38, 255),
		new Color(2, 143, 35, 255),
		new Color(2, 140, 38, 255),
		new Color(2, 138, 35, 255),
		new Color(2, 135, 38, 255),
		new Color(2, 133, 35, 255),
//		new Color(3, 115, 38, 255),
//		new Color(3, 105, 38, 255),
//		new Color(3, 95, 38, 255),
//		new Color(3, 85, 38, 255),
	};
    
    Color[] brown = {
    	new Color(80, 55, 0),
    	new Color(78, 54, 0),
    	new Color(76, 52, 0),
    	new Color(70, 50, 0),
//    	new Color(215, 50, 0),
//    	new Color(210, 50, 0),
//    	new Color(205, 50, 0),
    };
    
    private BufferedImage[] tiles = {
//    		createTile(greens),
//    		createTile(greens),
//    		createTile(greens),
//    		createTile(greens),
//    		createTile(greens, brown, 0, 0, 0, 0),
//    		createTile(greens, brown, 0, 0, 0, 0),
    		createTile(greens, brown, 0, 0, 0, 0), // 0: full
    		createTile(brown, greens, 1, 0, 0, 0), // 1: top
    		createTile(brown, greens, 1, 1, 0, 0), // 2: top-right
    		createTile(brown, greens, 0, 1, 0, 0), // 3: right
    		createTile(brown, greens, 0, 1, 1, 0), // 4: right-bottom
    		createTile(brown, greens, 0, 0, 1, 0), // 5: bottom
    		createTile(brown, greens, 0, 0, 1, 1), // 6: bottom-left
    		createTile(brown, greens, 0, 0, 0, 1), // 7: left
    		createTile(brown, greens, 1, 0, 0, 1), // 8: left-top
    		createTile(brown, greens, 0, 0, 0, 0), // 9: empty
    		
    		createTile(brown, greens, 1, 1, 0, 1), // 10: top free
    		createTile(brown, greens, 1, 1, 1, 0), // 11: right free
    		createTile(brown, greens, 0, 1, 1, 1), // 12: bottom free
    		createTile(brown, greens, 1, 0, 1, 1), // 13: left free
    		
    		createTile(brown, greens, 0, 1, 0, 1), // 14: 
    		createTile(brown, greens, 1, 0, 1, 0), // 15:
    		createTile(greens, brown, 1, 1, 1, 1), // 16:
    		
    		
//    		createTile3(greens, brown),
//    		createTile3(greens, brown),
//    		createTile3(greens, brown),
    		createTile(brown),
    		createTile(brown),
    		createTile(brown),
    		createTile(brown),
    };

    public BufferedImage createTile(Color[] colors) {
    	BufferedImage tile = new BufferedImage(GRID_SIZE, GRID_SIZE, BufferedImage.TYPE_INT_ARGB);
    	Graphics2D g2d = (Graphics2D) tile.getGraphics();
    	int quarter = GRID_SIZE/8;
    	for (int y = 0; y < 8; y++) {
        	for (int x = 0; x < 8; x++) {
        		int i = (int) Math.round(Math.random() * (colors.length-1));
        		g2d.setColor(colors[i]);
	    		g2d.fillRect(x*quarter, y*quarter, quarter, quarter);
    		}
    	}
    	g2d.dispose();
    	return tile;
    }
    
    public BufferedImage createTile2(Color[] colors, Color[] colors2) {
    	BufferedImage tile = new BufferedImage(GRID_SIZE, GRID_SIZE, BufferedImage.TYPE_INT_ARGB);
    	Graphics2D g2d = (Graphics2D) tile.getGraphics();
    	int quarter = GRID_SIZE/8;
    	for (int y = 0; y < 8; y++) {
        	for (int x = 0; x < 8; x++) {
        		if ( x == 4 && y == 4) {
        			int i = (int) Math.round(Math.random() * (colors.length-1));
            		g2d.setColor(colors[i]);
    	    		g2d.fillRect(x*quarter, y*quarter, quarter, quarter);
        			continue;
        		} 
        		if ( x > 3 && y > 3) {
        			int i = (int) Math.round(Math.random() * (colors2.length-1));
            		g2d.setColor(colors2[i]);
    	    		g2d.fillRect(x*quarter, y*quarter, quarter, quarter);
        			continue;
        		}
        		int i = (int) Math.round(Math.random() * (colors.length-1));
        		g2d.setColor(colors[i]);
	    		g2d.fillRect(x*quarter, y*quarter, quarter, quarter);
    		}
    	}
    	g2d.dispose();
    	return tile;
    }
    
    public BufferedImage createTile(Color[] colors1, Color[] colors2, int t, int r, int b, int l) {
    	BufferedImage tile = new BufferedImage(GRID_SIZE, GRID_SIZE, BufferedImage.TYPE_INT_ARGB);
    	
    	Graphics2D g2d = (Graphics2D) tile.getGraphics();
    	
    	int eight = GRID_SIZE / 8;

		for (int y = 0; y < 8; y++) {
        	for (int x = 0; x < 8; x++) {
        		int i = (int) Math.round(Math.random() * (colors1.length-1));
        		g2d.setColor(colors1[i]);
	    		g2d.fillRect(x*eight, y*eight, eight, eight);
        	}
    	}

    	if (t != 0) {
        	for (int x = 0; x < 8; x++) {
        		int i = (int) Math.round(Math.random() * (colors2.length-1));
        		g2d.setColor(colors2[i]);
	    		g2d.fillRect(x*eight, 0, eight, eight);
        	}
    	}
    	
    	if (r != 0) {
        	for (int y = 0; y < 8; y++) {
        		int i = (int) Math.round(Math.random() * (colors2.length-1));
        		g2d.setColor(colors2[i]);
	    		g2d.fillRect(7*eight, y*eight, eight, eight);
        	}
    	}
    	
    	if (b != 0) {
        	for (int x = 0; x < 8; x++) {
        		int i = (int) Math.round(Math.random() * (colors2.length-1));
        		g2d.setColor(colors2[i]);
	    		g2d.fillRect(x*eight, 7*eight, eight, eight);
        	}
    	}
		
    	if (l != 0) {
        	for (int y = 0; y < 8; y++) {
        		int i = (int) Math.round(Math.random() * (colors2.length-1));
        		g2d.setColor(colors2[i]);
	    		g2d.fillRect(0, y*eight, eight, eight);
        	}
    	}
    	
    	if (t != 0 && r != 0) {
    		int i = (int) Math.round(Math.random() * (colors2.length-1));
    		g2d.setColor(colors2[i]);
    		g2d.fillRect(6*eight, 1*eight, eight, eight);
    	}
    	
    	if (r != 0 && b != 0) {
    		int i = (int) Math.round(Math.random() * (colors2.length-1));
    		g2d.setColor(colors2[i]);
    		g2d.fillRect(6*eight, 6*eight, eight, eight);
    	}
    	
    	if (b != 0 && l != 0) {
    		int i = (int) Math.round(Math.random() * (colors2.length-1));
    		g2d.setColor(colors2[i]);
    		g2d.fillRect(1*eight, 6*eight, eight, eight);
    	}
    	
    	if (l != 0 && t != 0) {
    		int i = (int) Math.round(Math.random() * (colors2.length-1));
    		g2d.setColor(colors2[i]);
    		g2d.fillRect(1*eight, 1*eight, eight, eight);
    	}
    	
    	/*
			for (int y = 0; y < 8; y++) {
	        	for (int x = 0; x < 8; x++) {
	        		if ((x == 1  && y == 1) || (x == 1  && y == 6)
	        				|| (x == 6  && y == 1) || (x == 6  && y == 6)
	        				|| ((x == 0 || x == 7) && y >= 0 && y <= 7)
	        				|| ((y == 0 || y == 7) && x >= 0 && x <= 7)) {
	        			int i = (int) Math.round(Math.random() * (colors1.length-1));
	            		g2d.setColor(colors1[i]);
	    	    		g2d.fillRect(x*eight, y*eight, eight, eight);
	    	    		continue;
	        		}
	        		
	        		
	        		int i = (int) Math.round(Math.random() * (colors2.length-1));
	        		g2d.setColor(colors2[i]);
		    		g2d.fillRect(x*eight, y*eight, eight, eight);
	        	}
        	}
			    		
			
	    	for (int y = 0; y < 8; y++) {
	        	for (int x = 0; x < 8; x++) {
	        		
//	        		if (x >= 3 && x <= 4 &&  y >= 3 && y <= 4) {
//	        			int i = (int) Math.round(Math.random() * (colors1.length-1));
//	            		g2d.setColor(colors1[i]);
//	    	    		g2d.fillRect(x*eight, y*eight, eight, eight);
//	    	    		continue;
//	        		}
	        		if (t != 0 && x < 7 && y < 7) {
	        			int i = (int) Math.round(Math.random() * (colors2.length-1));
	            		g2d.setColor(colors2[i]);
	    	    		g2d.fillRect(x*eight, y*eight, eight, eight);
	        			continue;
	        		}
	        		if (r != 0 && x > 3 && y < 4) {
	        			int i = (int) Math.round(Math.random() * (colors2.length-1));
	            		g2d.setColor(colors2[i]);
	    	    		g2d.fillRect(x*eight, y*eight, eight, eight);
	        			continue;
	        		}
	          		if (b != 0 && x < 4 && y > 3) {
	        			int i = (int) Math.round(Math.random() * (colors2.length-1));
	            		g2d.setColor(colors2[i]);
	    	    		g2d.fillRect(x*eight, y*eight, eight, eight);
	        			continue;
	        		}
	          		if (l != 0 && x > 3 && y > 3) {
	        			int i = (int) Math.round(Math.random() * (colors2.length-1));
	            		g2d.setColor(colors2[i]);
	    	    		g2d.fillRect(x*eight, y*eight, eight, eight);
	        			continue;
	        		}
	        		int i = (int) Math.round(Math.random() * (colors1.length-1));
	        		g2d.setColor(colors1[i]);
		    		g2d.fillRect(x*eight, y*eight, eight, eight);
	    		}
	    	}
    	}*/
    	g2d.dispose();
    	return tile;
    }
    
    
    @Override
    public void init() {
//        video.window(0);
        setFrameRate(60);
        setWindowSize(GRID_SIZE*10, GRID_SIZE*8);
        restartVideo();
        
        setMouseVisible(true);
        setRelativeMouseMode(false);
        
        mapToKey(quit, KEY_ESC);
        
//        int[][] map = currentMapTerrainGrid;
//        for (int y = 0; y < map.length; y++) {
//        	for (int x = 0; x < map[y].length; x++) {
//        		map[y][x] = (int) Math.round(Math.random() * (2));
//        	}
//        }
        
        world.setTeamPlayer(TEAM_BLUE);
        
//        world.createEntity(UNIT_PEON, TEAM_RED,    "x", 50,  "y", 50);
//        world.createEntity(UNIT_PEON, TEAM_BLUE,   "x", 100, "y", 50);
//        world.createEntity(UNIT_PEON, TEAM_BLUE,   "x", 200, "y", 50);
//        world.createEntity(UNIT_PEON, TEAM_YELLOW, "x", 100, "y", 150);
//        world.createEntity(UNIT_PEON, TEAM_BLUE,   "x", 150, "y", 150);
        
    }

    @Override
    public void shutdown() {
        // TODO Auto-generated method stub
        
    }
    
    
    public void update(float elapsedTime) {
        if (quit.isPressed()) {
            stop();
            return;
        }

        if (isButtonReleased(MOUSE_BUTTON_LEFT)) {
        	int mx = (getMouseX() / GRID_SIZE);
            int my = (getMouseY() / GRID_SIZE);
        	currentMapTerrainGrid[my][mx] = 4 + (int) Math.round(Math.random() * (3));
        }
        
        world.action(this);
        world.update(elapsedTime);
        
    }
    
    @Override
    public void draw(Graphics2D g2d) {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.clearRect(0, 0, getWindowSize().width, getWindowSize().height);

        
        int[][] map = currentMapTerrainGrid;
        for (int y = 0; y < map.length; y++) {
        	for (int x = 0; x < map[y].length; x++) {
        		int i = map[y][x];
        		g2d.drawImage(tiles[i], x*GRID_SIZE, y*GRID_SIZE, null);
        		g2d.setColor(new Color(188, 196, 231, 128));
//        		g2d.setStroke(LINE_WIDTH_1);
////        		g2d.drawRect(x*GRID_SIZE, y*GRID_SIZE, GRID_SIZE, GRID_SIZE);
        	}
        }
        
        
        int mx = (getMouseX() / GRID_SIZE) * GRID_SIZE;
        int my = (getMouseY() / GRID_SIZE) * GRID_SIZE;
        
        g2d.setColor(new Color(0, 255, 0, 128));
		g2d.fillRect(mx, my, GRID_SIZE, GRID_SIZE);
        
        
      world.draw(drawer);
        
        
        String frames = String.format("Frames %s/%s", getFps(), getFrameRate());
        
        g2d.setColor(Color.WHITE);
        g2d.drawString(frames, 10, 10);
    }

}
